using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cinemachine;
using UnityEngine;
using UnityEngine.Events;

//定义放置功能
public class PropPlacementManager : MonoBehaviour
{
    [SerializeField, Header("道具的预制体")]
    private GameObject propPrefab;

    [SerializeField]
    [Header("需要放置的道具列表")]
    private List<ItemDataInfo> itemDataInfos;

    private GameObject itemDataParent;//物品父类

    [SerializeField, Header("敌人和玩家预制体")]
    private GameObject enemyPrefab, playerPrefab;
    [SerializeField, Header("虚拟相机")]
    private CinemachineVirtualCamera vCamera;

    //放置主角
    public void SetPlayer(ItemPlacementHelper itemPlacementHelper)
    {
        ClearPlayer();
        var position = itemPlacementHelper.GetItemPlacementPosition(PlacementType.OpenSpace, 10, new Vector2Int(1, 1), false);
        if (position == null) return;
        GameObject player = Instantiate(playerPrefab, (Vector3)position, Quaternion.identity);
        SpriteRenderer propSpriteRenderer = player.GetComponentInChildren<SpriteRenderer>();
        propSpriteRenderer.transform.localPosition = new Vector2(1, 1) * 0.5f;
        //使相机跟随玩家
        vCamera.Follow = player.transform;
        vCamera.LookAt = player.transform;
    }

    //清空主角
    private void ClearPlayer()
    {
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        if (player) DestroyImmediate(player);

    }

    //放置敌人
    public void SetEnemy(ItemPlacementHelper itemPlacementHelper)
    {
        ClearEnemy();
        
        //测试放置10个敌人
        for (int i = 0; i < 10; i++)
        {
            var position = itemPlacementHelper.GetItemPlacementPosition(PlacementType.OpenSpace, 10, new Vector2Int(1, 1), false);
            if (position == null) return;
            GameObject enemy = Instantiate(enemyPrefab, (Vector3)position, Quaternion.identity);
            SpriteRenderer propSpriteRenderer = enemy.GetComponentInChildren<SpriteRenderer>();
            propSpriteRenderer.transform.localPosition = new Vector2(1, 1) * 0.5f;
        }
    }

    //清空敌人
    private void ClearEnemy()
    {
        GameObject[] enemys = GameObject.FindGameObjectsWithTag("Enemy");
        foreach (GameObject enemy in enemys)
        {
            DestroyImmediate(enemy);
        }
    }

    // 放置物品
    public void SetData(ItemPlacementHelper itemPlacementHelper)
    {
        ClearData();

        foreach (var itemDataInfo in itemDataInfos)
        {
            int count = UnityEngine.Random.Range(itemDataInfo.minQuantity, itemDataInfo.maxQuantity + 1);
            for (int i = 0; i < count; i++)
            {
                var position = itemPlacementHelper.GetItemPlacementPosition(itemDataInfo.itemData.placementType, 10, itemDataInfo.itemData.size, itemDataInfo.itemData.addOffset);
                if (position != null)
                {
                    SetIteamData((Vector3)position, itemDataInfo);
                }
            }
        }
    }

    //清空物品
    private void ClearData()
    {
        itemDataParent = GameObject.Find("ItemDataParent");
        //清空物品
        if (itemDataParent) DestroyImmediate(itemDataParent);
        itemDataParent = new GameObject("ItemDataParent");
    }

    //放置物品
    private void SetIteamData(Vector3 position, ItemDataInfo itemDataInfo)
    {
        // 实例化道具对象
        GameObject prop = Instantiate(propPrefab, position, Quaternion.identity);

        //绑定父级
        prop.transform.SetParent(itemDataParent.transform);

        //修改名称
        prop.name = itemDataInfo.itemData.name;

        SpriteRenderer propSpriteRenderer = prop.GetComponentInChildren<SpriteRenderer>();

        // 添加碰撞体
        CapsuleCollider2D collider = propSpriteRenderer.gameObject.AddComponent<CapsuleCollider2D>();
        collider.offset = Vector2.zero;
        // 根据道具大小设置碰撞体方向
        if (itemDataInfo.itemData.size.x > itemDataInfo.itemData.size.y)
        {
            collider.direction = CapsuleDirection2D.Horizontal;
        }
        // 根据道具大小设置碰撞体大小
        Vector2 size = new Vector2(itemDataInfo.itemData.size.x * 0.8f, itemDataInfo.itemData.size.y * 0.8f);
        collider.size = size;

        // 设置道具的精灵图片
        propSpriteRenderer.sprite = itemDataInfo.itemData.sprite;
        //调整精灵图片的位置
        propSpriteRenderer.transform.localPosition = new Vector2(1, 1) * 0.5f;
    }
}